<?php
class Home extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('post_model');
        $this->load->model('user_model');
    }
    
    public function index() {
        if ($this->session->userdata('userinfo') == false) {
            redirect('');
        } else {
            redirect('home/' . $_SESSION['userinfo']['user_id']);
        }
    }
    
    public function portal() {
        $user_id = $this->input->post('user_id');
        if ($this->session->userdata('userinfo') == false) {
            $data['is_self'] = FALSE;
        } else {
            // 判断id是否为自己
            $data['is_self'] = ($user_id === $_SESSION['userinfo']['user_id']) ? TRUE : FALSE;
        }
        $html = $this->load->view('home/home_portal', $data, true);
        $this->output->set_output($html);
    }
    
    public function info() {
        $user_id = $this->input->post('user_id');
        if ($this->session->userdata('userinfo') == false) {
            $data['is_self'] = FALSE;
        } else {
            $data['is_self'] = ($user_id === $_SESSION['userinfo']['user_id']) ? TRUE : FALSE;
        }
        
        // preprocess data from db
        $this->load->helper('util');
        $dbinfo = $this->user_model->get_user_info($user_id);
        $info = array(
            'gender' => value_to_gender($dbinfo['gender']),
            'year_of_birth' => $dbinfo['year_of_birth'],
            'constel' => value_to_constel($dbinfo['constel']),
            'living_place' => $dbinfo['living_place'],
            'is_single' => value_to_is_single($dbinfo['is_single']),
            'biography' => $dbinfo['biography']
        );
        $data['info'] = $info;
        $html = $this->load->view('home/home_info', $data, true);
        $this->output->set_output($html);
    }
    
    public function album() {
        $user_id = $this->input->post('user_id');
        $albums = $this->post_model->get_albums($user_id);
        if ($albums !== NULL) {
            $data['albums'] = $albums;
        }
        $html = $this->load->view('home/home_album', isset($data) ? $data : NULL, true);
        $this->output->set_output($html);
    }
    
    // public function msg() {
        // $html = $this->load->view('home/home_msg', NULL, true);
        // $this->output->set_output($html);
    // }
    
    public function view($user_id = NULL) {
        $is_self = FALSE;
        
        if ($this->session->userdata('userinfo') != false) {
            if ($user_id === $_SESSION['userinfo']['user_id']) {
                $is_self = TRUE;
                $data = $_SESSION['userinfo'];
            }
        }
        
        if (!$is_self) {
            $data = $this->user_model->getUserInfoById($user_id);
        }
    
        if ($data !== NULL) {
            $data['is_self'] = $is_self;
            $this->load->view('header');
            $this->load->view('navbar');
            $this->load->view('home/home', $data);
            $this->load->view('footer');
        } else {
            show_404();
        }
    }
}
?>